{extend name="public:layout" /}
{block name="title"}赠品组合管理{/block}
{block name="head"}{/block}
{block name="body"}
<div class="wrapper wrapper-content">
    <div class="ibox">
        <div class="ibox-title">
            <ol class="breadcrumb">
                <li><i class="fa fa-hand-o-right"></i> 赠品组合列表</li>
            </ol>
        </div>
        <div class="ibox-content">
            <div class="toolbar">
                <div class="pull-left">
                    <a class="btn btn-primary" href="{:url('add')}">
                        <i class="fa fa-plus"></i>&nbsp;添加赠品组合
                    </a>
                </div>
                <div class="pull-right">
                    <form class="form-inline" method="post" id="searchForm">
                        <div class="form-group">
                            <input class="form-control" name="goods_name" value="{$query.goods_name}" type="text" placeholder="活动商品">
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-info">
                                <i class="fa fa-search"></i> 搜索
                            </button>
                            <a class="btn btn-white" href="{:url('index')}">
                                <i class="fa fa-trash"></i> 清空
                            </a>
                        </div>
                    </form>
                </div>
                <div class="clearfix"></div>
            </div>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th width="50">#</th>
                    <th>商品名称</th>
                    <th>赠品</th>
                    <th width="80">操作</th>
                </tr>
                </thead>
                <tbody>
                {volist name="list" id="vo"}
                <tr>
                    <td>{$vo.sku_id}</td>
                    <td>
                        <img src="{$vo.goods_thumb}" class="pull-left" style="width:32px;height:32px;margin:4px;" />
                        {$vo.goods_name} {$vo.sku_name}
                    </td>
                    <td>
                        {volist name="vo.item" id="item"}
                        <div class="clearfix">
                            <img src="{$item.gift_goods_thumb}" class="pull-left" style="width:32px;height:32px;margin:4px;" />
                            {$item.gift_goods_name} {$item.gift_sku_name} × {$item.gift_num}
                            <a href="javascript:;" onclick="delGoods(this)"
                               data-msg="确认删除吗？" data-url="{:url('delGoods')}" data-params="gift_id={$item.gift_id}">
                                <i class="fa fa-close text-danger"> </i>
                            </a>
                        </div>
                        {/volist}
                    </td>
                    <td>
                        <a class="btn btn-info" onclick="showOneGoods(this,setOneGoods);" data-field="{$vo.sku_id}" data-siteid="{$site_id}" data-url="{:url('plus/Goods/selectone')}">
                            添加赠品
                        </a>
                        <a class="btn btn-danger" href="javascript:;" onclick="doAction(this);"
                           data-msg="确认删除吗？" data-url="{:url('del')}" data-params="sku_id={$vo.sku_id}">删除组合</a>
                    </td>
                </tr>
                {/volist}
                </tbody>
            </table>
            <div class="page-bar">{$page}</div>
            <div class="clearfix"></div>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
    In('selectgoods');
    function delGoods(obj) {
        var url = $(obj).data('url');
        var params = $(obj).data('params');
        In('layer',function () {
            layer.confirm('确认删除吗？', {icon: 3, title: '提示消息'}, function (index) {
                layer.close(index);
                getAjax(url,params,function (res) {
                    if(res.code==1){
                        $(obj).closest('div').remove();
                    }else{
                        layer.alert(res.msg);
                    }
                })
            });
        })
    }

    /**
     * 单个增加赠品
     * @param field
     * @param sku
     */
    function setOneGoods(field,sku) {
        if(sku.sku_id>0){
            var sku_id = field;
            var html='<div class="clearfix" style="padding:10px;"><img class="pull-left" src="'+sku.sku_info.goods_thumb+'" style="width:80px;height:80px;"/>';
            html += sku.sku_info.goods_name+' '+sku.sku_info.sku_name+'</div>';
            html += '<div class="input-group" style="width:380px;">';
            html += '<span class="input-group-addon">赠送数量</span>';
            html += '<input type="text" class="form-control" id="gift_num" value="1" />';
            In('layer',function () {
                layer.open({
                    type: '1',
                    title: '设置赠品数量',
                    area:'380px',
                    closeBtn:0,
                    btn: ['确认','取消'],
                    content:html,
                    yes: function(index, layero){
                        var gift_num=$('#gift_num').val();
                        $.post("{:url('addGoods')}",{sku_id:sku_id,gift_sku_info:JSON.stringify(sku.sku_info),gift_num:gift_num},function (res) {
                            if(res.code==1){
                                rePage();
                            }else{
                                layer.alert(res.msg);
                            }
                        });
                    },
                    btn2: function(index, layero){
                        layer.close(index);
                    }
                });
            })
        }
    }
</script>
{/block}